import 'package:flutter/cupertino.dart';
import 'package:flutter_yn_goods/config/app_config.dart';
import 'package:flutter_yn_goods/config/app_routes.dart';
import 'package:flutter_yn_goods/config/app_translations.dart';
import 'package:flutter_yn_goods/generated/assets.dart';
import 'package:flutter_yn_goods/pages/home_view.dart';
import 'package:flutter_yn_goods/pages/mine_view.dart';
import 'package:flutter_yn_goods/pages/model/model_app_config.dart';
import 'package:flutter_yn_goods/pages/order/order_add/order_add_view.dart';
import 'package:flutter_yn_goods/pages/order/order_view.dart';
import 'package:flutter_yn_goods/user/app_user_manager.dart';
import 'package:flutter_yn_goods/utils/http/http_export.dart';
import 'package:flutter_yn_goods/utils/log/utils_log.dart';
import 'package:get/get.dart';

class MainLogic extends GetxController {
  final pageController = PageController(keepPage: true);
  final pages = <MainPageModel>[].obs;

  var currentIndex = 0.obs;

  int lastPopTime = 0;

  Worker? _userWorker;

  @override
  void onReady() {
    super.onReady();
  }

  @override
  void onInit() {
    super.onInit();

    addPageView();

    // _userWorker = ever(AppUserManager().user, (_) {
    //   clog("用户状态发生变化，重新构建页面列表");
    //
    //   addPageView();
    //
    //   if (currentIndex.value >= pages.length) {
    //     currentIndex.value = 0;
    //     if (pageController.hasClients) {
    //       pageController.jumpToPage(0);
    //     }
    //   }
    //
    //   update();
    // });

    AppConfig.getAppConfig();
  }

  void addPageView() {
    // pages.clear();

    pages.add(MainPageModel(
      iconName: Assets.tabBarRecommend,
      activeIconName: Assets.tabBarRecommendSel,
      name: LocaleKeys.recommend.tr,
      page: HomePage(),
    ));

    pages.add(MainPageModel(
      iconName: Assets.tabBarOrder,
      activeIconName: Assets.tabBarOrderSel,
      name: LocaleKeys.orders.tr,
      page: OrderPage(),
    ));
    // }

    pages.add(MainPageModel(
      iconName: Assets.tabBarMine,
      activeIconName: Assets.tabBarMineSel,
      name: LocaleKeys.mine.tr,
      page: MinePage(),
    ));
  }

  void selectIndex(int index) {
    if (currentIndex.value != index) {
      if (index == 1 && AppUserManager().isLogin == false) {
        Get.toNamed(AppRoutes.login);
        return;
      }

      currentIndex.value = index;
      pageController.jumpToPage(index);
      if (index == pages.length - 1 && AppUserManager().isLogin) {
        AppUserManager().refreshUserInfo();
      }
    }
  }

  @override
  void onClose() {
    _userWorker?.dispose();

    pageController.dispose();
    super.onClose();
  }
}

class MainPageModel {
  final String iconName;
  final String activeIconName;
  final String name;
  final dynamic page;

  MainPageModel({
    required this.iconName,
    required this.activeIconName,
    required this.name,
    required this.page,
  });
}
